#!/usr/bin/python2

# # Description/Docstring Here:
""" 
Purpose: This app is a die roller that also incorporates the Savage worlds
wild dice mechanic.
"""

# # Metadata Here:
__author__ = "Ken Stulce"
__date__ = "12/05/2013"
__copyright__ = "Copyright 2013, Ken Stulce"
__credits__ = [""]
__license__ = "GPL"
__version__ = "0.0.2"
__maintainer__ = "Ken Stulce"
__email__ = "kstulce@gmail.com"
__status__ = "Prototype"
# # Python version 2.7x

# # Imports Here:
import random

class die:
    """ Class represents a playing die """
    nbr_of_sides = 0
    rolls_count = 0
    current_val = 0
    
    def roll_die(self, sides):
        """ Method generates a random number between 0 and the die value """
        self.current_val = random.randint(1, sides)
        self.rolls_count =+ 1
        return self.current_val

class swDieRoller(die):
    """ Class represents rolling SW dice with a 'wild die'.  End-user
        provides a die type to initiate the roll.
    """
    nbr_of_dice = 0    
    nbr_of_rolls = 0
    wild_die = 6  # This is SW wild die type CONSTANT
    wild_die_val = 0
    eu_val = 0
    cur_wild_roll = 0
    cur_eu_roll = 0
    excepted_dice = [4, 6, 8, 10, 12, 20, 100]
    
    def __init__(self):
        """ Get die type from end-user """
        # # Instantiate 
        eu_input = raw_input("Enter your die type (4, 6, 8, 10, 12, 20, 100) or \"Q\" to quit, please. ")
        print "You entered: " + eu_input
        wrong_entry = 1  # Var used to create a infinite loop
        while wrong_entry == 1:  # infinite loop
            if eu_input == "Q":  # Exit loop
                print "Quitting Now"
                break
            
            if self.validateEUDie(int(eu_input)):
                self.nbr_of_sides = eu_input
                self.rollWildDie()
                self.rollEUDie()
                
            eu_input = raw_input("Enter your die type (4, 6, 8, 10, 12, 20, 100) or \"Q\" to quit, please. ")
            print "You entered: " + str(eu_input)
                #self.roll_die(self.nbr_of_sides)
        
    def validateEUDie(self, dietype):
        """ Validate the end-user input for die type selection """
        for die in self.excepted_dice:
            print "Validating input Test: " + str(die)            
            if die == dietype:
                print "d" + str(die)
                return True
        
        if die != dietype:
            print "Please enter an appropriate die type!"
            
            return False
    
    def explodeDie(self, die, cur_val):
        """ Continue to roll the die if die explodes """
        print "Ace, ROLL AGAIN!"
        while cur_val == die:
            cur_roll = self.roll_die(die)
            print "Explode Roll: " + str(cur_roll)
            cur_val += cur_roll
            self.nbr_of_rolls += 1
            
            if cur_roll != die:
                print "Explosive results: " + str(cur_val)
                return cur_val
                break
    
    def rollWildDie(self):
        """ Roll the wild die, allowing for aces and exploding dice """
        #print str(self.wild_die)  # # DEBUG
        self.cur_wild_roll = self.roll_die(self.wild_die)
        print "And the current wild die roll is: " + str(self.cur_wild_roll)
        self.wild_die_val = self.cur_wild_roll
        self.nbr_of_rolls += 1
        
        # # Reroll if roll is die max val, SW rules self.nbr_of_sides
        if self.cur_wild_roll == self.wild_die:            
            # # Die exploded, rolling again
            self.wild_die_val = self.explodeDie(self.wild_die, self.cur_wild_roll)
            print "Wild die results: " + str(self.wild_die_val)  
            return self.wild_die_val 
            
        # # Return roll results
        elif (self.cur_wild_roll < self.wild_die):
            print "Wild die results: " + str(self.wild_die_val)  
            return self.wild_die_val                     

    def rollEUDie(self):
        """ Roll the EU die, allowing for aces and exploding dice """
        #print str(self.)  # # DEBUG
        self.cur_eu_roll = self.roll_die(int(self.nbr_of_sides))
        print "And the current EU roll is: " + str(self.cur_eu_roll)
        self.eu_val =+ self.cur_eu_roll
        self.nbr_of_rolls =+ 1
        
        # # Reroll if roll is die max val, SW rules
        if self.cur_eu_roll == int(self.nbr_of_sides):            
            # # Die exploded, rolling again
            self.eu_val = self.explodeDie(int(self.nbr_of_sides), self.cur_eu_roll)
            print "EU results: " + str(self.eu_val)  
            return self.eu_val 
            
        # # Return roll results
        elif (self.cur_eu_roll < int(self.nbr_of_sides)):
            print "EU results: " + str(self.eu_val)  
            return self.eu_val                     

# # Instantiate SWDieRoller()
roll = swDieRoller()